using System;
using System.Data;
using System.Web.UI.WebControls;

namespace SystemFrameworks.Util
{
	/// <summary>
	/// Summary description for ListControlHelp.
	/// </summary>
	public class ListControlHelp
	{
		public ListControlHelp()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		private const string HEAD_SELECT = "--Select--";
		private const string HEAD_NOSELECT = "";
		private const string HEAD_SELECT_ALL = "--Select All--";

	
		/// <summary>
		/// set the droplist by the key 
		/// </summary>
		/// <param name="key"></param>
		/// <param name="ctrl"></param>
		public static void setCtrlValue(string key, ListControl ctrl)
		{
			string sTemp = string.Empty;

			ListItem selectedItem = ctrl.SelectedItem;
			if ( selectedItem != null )
			{
				selectedItem.Selected = false;
			}

			for(int i=0;i<ctrl.Items.Count;i++)
			{ 
				sTemp=ctrl.Items[i].Value.Trim();
				if(key.Trim().ToUpper().Equals(sTemp.Trim().ToUpper()))
				{
					ctrl.Items[i].Selected=true;
					return;
				}
			}
		}
		
		/// <summary>
		/// Bind data to DropDownList
		/// </summary>
		/// <param name="ds"></param>
		/// <param name="ddl"></param>
		/// <param name="dataTextField">Column name to DataTextField</param>
		/// <param name="dataValueField">Column mane to DataValueField</param>
		public static void setCtrlValuesByDs( DataSet ds, DropDownList ddl, string dataTextField, string dataValueField )
		{
			setCtrlValuesByDs( ds, ddl, dataTextField, dataValueField, false, false );
		}


	
		/// <summary>
		/// Bind data to DropDownList
		/// </summary>
		/// <param name="ds"></param>
		/// <param name="ddl"></param>
		/// <param name="dataTextField">Column name to DataTextField</param>
		/// <param name="dataValueField">Column mane to DataValueField</param>
		public static void setCtrlValuesByDsDefault( DataSet ds, DropDownList ddl, string dataTextField, string dataValueField, string keyvalue )
		{
			ddl.Items.Clear();
			if ( ds != null && ds.Tables.Count > 0 )
			{
				DataView dv = new DataView( ds.Tables[ 0 ] );

				

			

				string textField = string.Empty;
				string valueField = string.Empty;

				for( int i=0; i< dv.Count; i++ )
				{
					textField = Convert.ToString( dv[ i ][ dataTextField ] ).Trim();
					valueField = Convert.ToString( dv[ i ][ dataValueField ] ).Trim();
					ddl.Items.Add( new ListItem( textField, valueField ) );	
					if (valueField.Equals(keyvalue))
						ddl.Items[i].Selected=true;
				}	
			}

			
		}



		
		/// <summary>
		/// Bind data to DropDownList
		/// </summary>
		/// <param name="ds"></param>
		/// <param name="ddl"></param>
		/// <param name="dataTextField">Column name to DataTextField</param>
		/// <param name="dataValueField">Column mane to DataValueField</param>
		public static void setCtrlValuesByDs( DataSet ds, DropDownList ddl, string dataTextField, string dataValueField, bool nonselective, bool isDesc )
		{
			ddl.Items.Clear();
			if ( ds != null && ds.Tables.Count > 0 )
			{
				DataView dv = new DataView( ds.Tables[ 0 ] );

				if ( isDesc )
				{
					dv.Sort = dataTextField + " DESC ";
				}
				else
				{
					dv.Sort = dataTextField;
				}

				ddl.Items.Add( new ListItem( HEAD_SELECT, string.Empty ));

				string textField = string.Empty;
				string valueField = string.Empty;

				for( int i=0; i< dv.Count; i++ )
				{
					textField = Convert.ToString( dv[ i ][ dataTextField ] ).Trim();
					valueField = Convert.ToString( dv[ i ][ dataValueField ] ).Trim();
					ddl.Items.Add( new ListItem( textField, valueField ) );	
				}	
			}

			if ( !nonselective )
			{
				// Set default select index
				setSelectDefault( ddl );
			}
		}


		public static void setCtrlValuesByDs( DataSet ds, DropDownList ddl, string dataTextField, string dataValueField, bool nonselective, bool isDesc,bool isEmpty )
		{
			ddl.Items.Clear();
			if ( ds != null && ds.Tables.Count > 0 )
			{
				DataView dv = new DataView( ds.Tables[ 0 ] );

				if ( isDesc )
				{
					dv.Sort = dataTextField + " DESC ";
				}
				else
				{
					dv.Sort = dataTextField;
				}
				
				ddl.Items.Add( new ListItem( HEAD_NOSELECT, string.Empty ));

				string textField = string.Empty;
				string valueField = string.Empty;

				for( int i=0; i< dv.Count; i++ )
				{
					textField = Convert.ToString( dv[ i ][ dataTextField ] ).Trim();
					valueField = Convert.ToString( dv[ i ][ dataValueField ] ).Trim();
					ddl.Items.Add( new ListItem( textField, valueField ) );	
				}	
			}

			if ( !nonselective )
			{
				// Set default select index
				setSelectDefault( ddl );
			}
		}
		
		/// <summary>
		/// Bind data to DropDownList
		/// </summary>
		/// <param name="ds"></param>
		/// <param name="ddl"></param>
		/// <param name="dataTextField">Column name to DataTextField</param>
		/// <param name="dataValueField">Column mane to DataValueField</param>
		public static void setCtrlValuesByDs( DataSet ds, DropDownList ddl, string dataTextField, string dataValueField, bool nonselective )
		{
			setCtrlValuesByDs( ds, ddl, dataTextField, dataValueField, nonselective, false );
		}

		/// <summary>
		/// Set default select index
		/// </summary>
		/// <param name="ddl"></param>
		private static void setSelectDefault( DropDownList ddl )
		{
			if ( ddl != null && ddl.Items.Count == 2 )
			{
				ddl.SelectedIndex = 1;	
			}
		}

	}
}
